package 中等;

import java.util.ArrayList;
import java.util.List;

public class N皇后问题 {
    public static List<int[]> list = new ArrayList<>();


    public static void main(String[] args) {
        get(4);
        for (int[] ints : list) {
            for (int anInt : ints) {
                System.out.print(anInt+"-");
            }
            System.out.println();
        }


    }

    public static void demo(){
        int[] arr = {1,3,5};
        //list1.add(arr);
    }

    public static void get (int n){
        int[] arr = new int[n];
        set(0,n ,arr);

    }

    public static void  set(int start,int n,int[] arr){
        if (start==n){
            int[] arr1 = new int[n];
            System.arraycopy(arr,0 ,arr1 ,0 ,n );
            list.add(arr1);
            return;
        }
        for (int i = 1; i < n+1; i++) {
            if (is(arr,start , i)){
                arr[start] = i;
                set(start+1,n ,arr);
                arr[start] = 0;
            }
        }

    }

    public static boolean is(int[] arr, int x,int y){
        for (int i = 0; i < x; i++) {
            if (arr[i]==y){
                return false;
            }
            if (x-i==Math.abs(arr[i]-y)){
                return false;
            }
        }
        return true;
    }
}
